package umls.data;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import jdbc.UMLS_DB_Interface;

public class Term {
	private String TermName;
	private TreeMap<String, ConceptCandidate> Concepts;

	public Term(String termname) {
		this.TermName = termname;
		this.Concepts = new TreeMap<String, ConceptCandidate>();
	}

	public void SetTerm(String termname) {
		this.TermName = termname;
	}

	public String GetTerm() {
		return this.TermName;
	}

	// guarda qui
	public void CreateConcepts(UMLS_DB_Interface umlsdb) throws SQLException {
		if (TermName != null && TermName != "") {
			ArrayList<String> cuis = umlsdb.GetCUIsListbyName(this.TermName);

			Iterator<String> cuiit = cuis.iterator();

			while (cuiit.hasNext()) {
				String CUI = cuiit.next();

				ArrayList<String> preferredTerms = umlsdb
						.GetPreferedNameByCUI(CUI);

				Iterator<String> preferredTerms_it = preferredTerms.iterator();

				while (preferredTerms_it.hasNext()) {
					String preferredterm = preferredTerms_it.next();

					ConceptCandidate conceptcandidate = new ConceptCandidate(
							CUI, preferredterm);

					// this method expands concepts
					// conceptcandidate.CreateExpandedConcepts(umlsdb);

					// this method expands concepts including the semantic type
					conceptcandidate
							.CreateExpandedConceptsBySemanticType(umlsdb);

					Concepts.put(preferredterm, conceptcandidate);
				}
			}
		}
	}

	public ArrayList<String> GetCompleteRelatedTermList() {
		ArrayList<String> result = new ArrayList<String>();

		Set conceptset = this.Concepts.entrySet();

		Iterator conceptset_it = conceptset.iterator();

		// Iterate through concept candidate
		while (conceptset_it.hasNext()) {
			Map.Entry<String, ConceptCandidate> entry = (Map.Entry<String, ConceptCandidate>) conceptset_it
					.next();

			ConceptCandidate concept = (ConceptCandidate) entry.getValue();

			ArrayList<String> temp = concept.GetAllPreferredTermsList();

			Iterator<String> temp_it = temp.iterator();

			while (temp_it.hasNext()) {
				result.add(temp_it.next());
			}

		}

		return result;
	}

	// usata guarda qui
	public HashSet<String> GetUniqueCompleteRelatedTermList() {
		HashSet<String> result = new HashSet<String>();

		Set conceptset = this.Concepts.entrySet();

		Iterator conceptset_it = conceptset.iterator();

		// Iterate through concept candidate
		while (conceptset_it.hasNext()) {
			Map.Entry<String, ConceptCandidate> entry = (Map.Entry<String, ConceptCandidate>) conceptset_it
					.next();

			ConceptCandidate concept = (ConceptCandidate) entry.getValue();

			ArrayList<String> temp = concept.GetAllPreferredTermsList();

			Iterator<String> temp_it = temp.iterator();

			while (temp_it.hasNext()) {
				result.add(temp_it.next());
			}

		}

		return result;
	}

	// this method returns the list of related terms according to the Semantic
	// Type passed through the hash
	public HashSet<String> GetUniqueCompleteRelatedTermListFilteredBySemType(
			SemGroupConfReader semgroupconfreader) {
		HashSet<String> result = new HashSet<String>();

		Set conceptset = this.Concepts.entrySet();

		Iterator conceptset_it = conceptset.iterator();

		// Iterate through concept candidate
		while (conceptset_it.hasNext()) {
			Map.Entry<String, ConceptCandidate> entry = (Map.Entry<String, ConceptCandidate>) conceptset_it
					.next();

			ConceptCandidate concept = (ConceptCandidate) entry.getValue();

			ArrayList<String> temp = concept
					.GetAllPreferredTermsListFilteredBySemType(semgroupconfreader);

			Iterator<String> temp_it = temp.iterator();

			while (temp_it.hasNext()) {
				result.add(temp_it.next());
			}

		}

		return result;
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");

		result.append("TermName: " + this.TermName + NEW_LINE);

		Set conceptset = this.Concepts.entrySet();

		Iterator conceptset_it = conceptset.iterator();

		while (conceptset_it.hasNext()) {
			Map.Entry<String, ConceptCandidate> entry = (Map.Entry<String, ConceptCandidate>) conceptset_it
					.next();

			ConceptCandidate concept = (ConceptCandidate) entry.getValue();

			result.append("Concept List: " + NEW_LINE + concept);
		}

		return result.toString();
	}

}
